package com.lxmes.gs.controller;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.lxmes.gs.domain.costAnalysisVO.*;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.enums.BusinessType;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.lxmes.gs.domain.CostAnalysis;
import com.lxmes.gs.service.ICostAnalysisService;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.common.core.page.TableDataInfo;

/**
 * 成本分析Controller
 *
 * @author wdf
 * @date 2023-04-24
 */
@Api(tags = "成本分析")
@RestController
@RequestMapping("/gs/costAnalysis")
public class CostAnalysisController extends BaseController{
    @Autowired
    private ICostAnalysisService costAnalysisService;

    /**
     * 查询成本分析列表
     */
    @ApiOperation("查询成本分析列表")
    @PreAuthorize("@ss.hasPermi('gs:costAnalysis:list')")
    @GetMapping("/list")
    public TableDataInfo list(CostAnalysisSearchVO costAnalysis){
        startPage();
        List<CostAnalysis> list = costAnalysisService.selectCostAnalysisList(BeanUtil.copyProperties(costAnalysis,CostAnalysis.class));
        return getDataTable(Convert.convert(new TypeReference<List<CostAnalysisResultVO>>() {},list));
}

    /**
     * 导出成本分析列表
     */
    @ApiOperation("导出成本分析列表")
    @PreAuthorize("@ss.hasPermi('gs:costAnalysis:export')")
    @Log(title = "成本分析", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CostAnalysis costAnalysis){
        List<CostAnalysis> list = costAnalysisService.selectCostAnalysisList(costAnalysis);
        List<CostAnalysisExportVO> exportList = Convert.convert(new TypeReference<List<CostAnalysisExportVO>>() {}, list);
        ExcelUtil<CostAnalysisExportVO> util = new ExcelUtil<CostAnalysisExportVO>(CostAnalysisExportVO.class);
        util.exportExcel(response, exportList, "成本分析数据");
    }

    /**
     * 获取成本分析详细信息
     */
    @ApiOperation("获取成本分析详细信息")
    @PreAuthorize("@ss.hasPermi('gs:costAnalysis:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id){
        return AjaxResult.success(costAnalysisService.selectCostAnalysisById(id));
    }

    /**
     * 新增成本分析
     */
    @ApiOperation("新增成本分析")
    @PreAuthorize("@ss.hasPermi('gs:costAnalysis:add')")
    @Log(title = "成本分析", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CostAnalysisCreateVO costAnalysis){
        return toAjax(costAnalysisService.insertCostAnalysis(BeanUtil.copyProperties(costAnalysis,CostAnalysis.class)));
    }

    /**
     * 修改成本分析
     */
    @ApiOperation("修改成本分析")
    @PreAuthorize("@ss.hasPermi('gs:costAnalysis:edit')")
    @Log(title = "成本分析", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CostAnalysisUpdateVO costAnalysis){
        return toAjax(costAnalysisService.updateCostAnalysis(BeanUtil.copyProperties(costAnalysis,CostAnalysis.class)));
    }

    /**
     * 删除成本分析
     */
    @ApiOperation("删除成本分析")
    @PreAuthorize("@ss.hasPermi('gs:costAnalysis:remove')")
    @Log(title = "成本分析", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids){
        return toAjax(costAnalysisService.deleteCostAnalysisByIds(ids));
    }
    /**
     * 导入成本分析
     */
    @Log(title = "成本分析", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception{
        ExcelUtil<CostAnalysisImportVO> util = new ExcelUtil<>(CostAnalysisImportVO.class);
        List<CostAnalysisImportVO> costAnalysisList = util.importExcel(file.getInputStream());
        String message = costAnalysisService.imporIteminfo(Convert.convert(new TypeReference<List<CostAnalysis>>() {},costAnalysisList), updateSupport);
        return AjaxResult.success(message);
    }
    /**
     * 下载导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException{
        ExcelUtil<CostAnalysisImportVO> util = new ExcelUtil<>(CostAnalysisImportVO.class);
        util.importTemplateExcel(response,"成本分析");
    }
}
